<?php
function timeAgo($datetime) {
  $now = new DateTime('now', new DateTimeZone('Asia/Shanghai'));
  $then = new DateTime($datetime, new DateTimeZone('Asia/Shanghai'));
  $diff = $now->diff($then);

  // 处理未来时间
  if ($diff->invert === 0) {
    return '即将到来';
  }

  if ($diff->y > 0) {
    return $diff->y . '年前';
  } elseif ($diff->m > 0) {
    return $diff->m . '个月前';
  } elseif ($diff->d >= 7) {
    return floor($diff->d / 7) . '周前';
  } elseif ($diff->d > 0) {
    return $diff->d . '天前';
  } elseif ($diff->h > 0) {
    return $diff->h . '小时前';
  } elseif ($diff->i > 0) {
    return $diff->i . '分钟前';
  } elseif ($diff->s > 5) {
    return $diff->s . '秒前';
  } else {
    return '刚刚';
  }
}
?>